package fr.insta.domaine;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;




public class ListeSimple<T> 
{
    
    private T value;
    private ChaineElement dernier_element;
    private ChaineElement premier_element = null; 
    private ChaineElement pointer;
    private int size = 0;
    private int counter = 0;
   



    public boolean add(Object arg0) {
    
        if (premier_element == null){
            premier_element = new ChaineElement();
            premier_element.setValue(arg0);
            dernier_element= premier_element;
            
        }
        else 
        {
                    ChaineElement t = new ChaineElement();
                    t.setValue(arg0);
                    dernier_element.setNext(t);
                    dernier_element = t;
        }
        size++;
        return true;
    }


    public void add(int arg0, Object arg1) {
    
        // TODO Auto-generated method stub
        
    }


    public boolean addAll(Collection arg0) {

            for (Iterator iterator = arg0.iterator(); iterator.hasNext();) {
                Object obj = (Object) iterator.next();
                ChaineElement t = new ChaineElement();
                t.setValue(obj);
                if (dernier_element == null){
                    // liste est vide
                    premier_element = t;
                    dernier_element = t;
                }
                dernier_element.setNext(t); 
                add(t);
            }
            return true;
    }

    public boolean addAll(int arg0, Collection arg1) 
    {
        for (Iterator iterator = arg1.iterator(); iterator.hasNext();) 
        {
            Object obj = (Object) iterator.next();
            add(obj);
        }
        return false;
    }

    public void clear() {
    
        premier_element=null;
        dernier_element=null;
        size=0;
        
    }


    public boolean contains(Object arg0) {
    
        boolean back = false;
        pointer = premier_element;
        for(int i =0;  i<size; i++){
            if (pointer.getValue().equals(arg0)){ 
                back = true;                
            }
            pointer = pointer.next();   
        }
      return back;  
    }


    public boolean containsAll(Collection arg0) {
        for (Object object : arg0) {
            if (!contains(object)) return false;
        }
        return true;
    }


    public Object get(int arg0) {
        ChaineElement cache = premier_element;
        for (int i=0 ; i<size; i++){
            
            if (i == arg0){
                return cache.getValue();
            }
            cache = cache.next();
            
        }
        return null;
    }


    public int indexOf(Object arg0) {
    
        ChaineElement cache = premier_element;
        for (int i=0 ; i<size; i++){
            
            if (arg0.equals(cache.getValue())){
                return i;
            }
            cache = cache.next();
            
        }
        return 44;
    }


    public boolean isEmpty() {
    
        if (size==0) 
            return true;
        return false;
    }


    public Iterator iterator() {
        return null;
    }


    public int lastIndexOf(Object arg0) {
        
        int data = 1000;
        ChaineElement cache = premier_element;
        for (int i=0; i<size; i++){
           
            if (arg0.equals(cache.getValue())){
                data = i;
            }
            cache = cache.next();
        }
        return data;
    }


    public ListIterator listIterator() {    
        return null;
    }


    public ListIterator listIterator(int arg0) {    
        return null;
    }


    public boolean remove(Object arg0) {     
        if (!(size <= 0)) {
            ChaineElement cache = premier_element;
            for (int i = 0; i<=size; i++){
                if (arg0.equals(cache.getValue())){
                    remove(i);
                }
                cache = cache.next();
            }
        }
        
        return true;
    }


    public Object remove(int arg0) {
        ChaineElement precedent = premier_element;
        ChaineElement cache = premier_element;
        for (int i=0 ; i<=arg0; i++){
            
            if (i == arg0){
                precedent.setNext(cache);
                size--;
                return cache;
            }
            precedent = cache;
            cache = cache.next();
        }
        return null;
    }


    public boolean removeAll(Collection arg0) {
    
        for (Iterator iterator = arg0.iterator(); iterator.hasNext();) {
            Object object = (Object) iterator.next();
            remove(object);
        }
        return true;
    }


    public boolean retainAll(Collection arg0) {
    
        // TODO Auto-generated method stub
        return false;
    }


    public Object set(int arg0, Object arg1) {
        ChaineElement cache = premier_element;
        for (int i=0 ; i<=size; i++){
            
            if (arg0 == i){
                cache.setValue(arg1);
            }
            else cache = cache.next();
         }    
            return cache;
   }


    public int size() {
        
        return size;
    }


    public ListeSimple subList(int arg0, int arg1) {
    
        ListeSimple back = new ListeSimple();
        ChaineElement data = premier_element; 
        for (int i = 0; i < size; i++) {
            if (i <arg1 && i>=arg0){
                back.add(data);
            }
            data = data.next();
        }
        return back;
    }


    public <E> E[] toArray() {
        Object[] back = new Object[size];
        ChaineElement data = premier_element; 
        for (int i = 0; i < size; i++) {
                back[i] = (E) data.getValue();
                data = data.next();
            }
        
        return (E[]) back;
    }


    public <E> E[] toArray(E[] back) {
        
        if (back.length < size()) {
            back = (E[]) Arrays.copyOf(back, size, back.getClass());
        }
        
        ChaineElement data = premier_element; 
        for (int i = 0; i < size; i++) {
                back[i] = (E)data.getValue();
                data = data.next();
            }
        
        return back;
    }
    
   
    
    
    
}
